package com.robb19y.rgbdscanner.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class ColorFilter implements Serializable{
	private static final long serialVersionUID = 1L;
	private double hMax;
	private double hMin;
	private double sMax;
	private double sMin;
	private double lMax;
	private double lMin;
	
	public ColorFilter(double hMax, double hMin, double sMax, double sMin,
			double lMax, double lMin) {
		super();
		this.hMax = hMax;
		this.hMin = hMin;
		this.sMax = sMax;
		this.sMin = sMin;
		this.lMax = lMax;
		this.lMin = lMin;
	}

	public double gethMax() {
		return hMax;
	}

	public double gethMin() {
		return hMin;
	}

	public double getsMax() {
		return sMax;
	}

	public double getsMin() {
		return sMin;
	}

	public double getlMax() {
		return lMax;
	}

	public double getlMin() {
		return lMin;
	}

	@Override
	public String toString() {
		return "ColorFilter [hMax=" + hMax + ", hMin=" + hMin + ", sMax="
				+ sMax + ", sMin=" + sMin + ", lMax=" + lMax + ", lMin=" + lMin
				+ "]";
	}
	
	public String writeToFile(){
		Date date = new Date();
		try{
			FileOutputStream fout = new FileOutputStream("ColorFilter_"+date.toString());
	        ObjectOutputStream oos = new ObjectOutputStream(fout);
	        oos.writeObject(this);
	        oos.close();
		}catch(Exception e){
			System.out.println("Can not write filter to file.");
		}
		return "ColorFilter_"+date.toString();
	}
	
	public static ColorFilter readFromFile(String file){
		ColorFilter readCase = null;
		try{
			FileInputStream streamIn = new FileInputStream(file);
	        ObjectInputStream objectinputstream = new ObjectInputStream(streamIn);
	        readCase = (ColorFilter) objectinputstream.readObject();
	        objectinputstream.close();
		}catch(Exception e){
			System.out.println("Can not read filter from file.");
		}
		return readCase;
		
	}
	
	public static double[] rgbToHsl(double[] rgbColor){
		double r1 = (double) rgbColor[0]; 
		double g1 = (double) rgbColor[1];
		double b1 = (double) rgbColor[2];
		
	    r1 = r1/255;
	    g1 = g1/255;
	    b1 = b1/255;
	    
	    double max = Math.max(r1, Math.max(g1, b1));
	    double min = Math.min(r1, Math.min(g1, b1));
	    double l = (max + min) / 2;
	    double h;
	    double s;
	    
	    if(max == min){
	        h = s = 0; 
	    }else{
	        double d = max - min;
	        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
	        
	        if(max == r1){
	        	h = (g1 - b1) / d + (g1 < b1 ? 6 : 0);
	        }else if(max == g1){
	        	h = (b1 - r1) / d + 2;
	        }else{
	        	h = (r1 - g1) / d + 4;
	        }	        

	        h /= 6;
	    }	    
	    return new double[]{h, s, l};
	}
	
	public boolean filter(double[] rgbColor){
		double[] hsl = ColorFilter.rgbToHsl(rgbColor);
		
		return hsl[0] >= hMin && hsl[0] <= hMax && 
				hsl[1] >= sMin && hsl[1] <= sMax &&
				 hsl[2] >= lMin && hsl[2] <= lMax;
	}

}
